//
//  EventTest.swift
//  SwiftTools
//
//  Created by Samuel Richard Magny on 2015-09-09.
//  Copyright (c) 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa
import SwiftTools
import XCTest

class EventTest: XCTestCase {

    override func setUp() {
        super.setUp()
        resetRaised()
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    var event1Raised:Bool = false
    var event2Raised:Bool = false
    func testRaisingEvent() {
        
        let event = Event<Bool>()
        let handler = event.addHandler(self, handler: EventTest.handleEvent1)
        
        event.raise(true)
        handler.dispose()
        
        XCTAssert(event1Raised, "The event raised")
    }
    
    
    func testTwoEventActIndependently() {
        
        let event1 = Event<Bool>()
        let event2 = Event<Bool>()
        let handler1 = event1.addHandler(self, handler: EventTest.handleEvent1)
        let handler2 = event2.addHandler(self, handler: EventTest.handleEvent2)
        
        event1.raise(true)
        XCTAssert(event1Raised, "The event1 raised")
        XCTAssert(!event2Raised, "The event2 didn't raised")
        
        resetRaised()
        
        event2.raise(true)
        XCTAssert(event2Raised, "The event2 raised")
        XCTAssert(!event1Raised, "The event1 didn't raised")
        
        resetRaised()
        
        event1.raise(true)
        event2.raise(true)
        XCTAssert(event1Raised, "The event1 raised")
        XCTAssert(event2Raised, "The event2 raised")

        handler1.dispose()
        handler2.dispose()
    }
    
    func handleEvent1(data: Bool) {
        event1Raised = true
    }
    
    func handleEvent2(data: Bool) {
        event2Raised = true
    }
    
    func resetRaised()
    {
        event1Raised = false
        event2Raised = false
    }

}
